package com.zjf.fupload.util;

import java.io.File;

/**
 * @author zjf
 */
public class FileUtil {
	
	public static String getExtension(String fileName) {
		int dotLastIndex = fileName.lastIndexOf('.');
		if(dotLastIndex >= 0) {
			if(fileName.length() == dotLastIndex) {
				return "";
			}
			return fileName.substring(dotLastIndex + 1);
		}
		return "";
	}
	
	public static long getSize(File[] files) {
		
		long total = 0l;
		for(File f : files) {
			total += getSize(f);
		}
		
		return total;
		
	}

	/**
	 * 获取文件、目录大小
	 * @param file
	 * @return
	 */
	public static long getSize(File file) {
		
		if(file.exists()) {
			
			if(file.isFile()) {
				return file.length();
			} else if(file.isDirectory()) {
				
				int size = 0;
				File[] subFiles = file.listFiles();
				for(File subFile : subFiles) {
					size += getSize(subFile);
				}
				
			}
			
		}
		
		System.out.println("文件不存在，文件大小返回0，文件路径：" + file.getAbsolutePath());
		
		return 0l;
		
	}
	
}
